//
//  LZNormalPlayerVC.swift
//  testZD
//
//  Created by 湛亚磊 on 2020/9/23.
//  Copyright © 2020 湛亚磊. All rights reserved.
//
///普通视频播放
import UIKit

open class LZNormalPlayerVC: UIViewController {
    public lazy var topView: UIImageView = {
        let topView=UIImageView(frame: CGRect(x: 0, y: 100, width: UIScreen.main.bounds.size.width, height: 200))
        topView.setImageWithURLString("https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240", placeholder: ZFUtilities.image(with: UIColor.gray, size: CGSize(width: 1, height: 1)))
        return topView
    }()
    public lazy var player: ZFPlayerController = {
        let player=ZFPlayerController(playerManager: playerManager, containerView: topView)
        player.controlView=controlView
        player.pauseWhenAppResignActive = false
        return player
    }()
    
    public lazy var playerManager: ZFAVPlayerManager = {
        let playerManager:ZFAVPlayerManager=ZFAVPlayerManager()
        playerManager.shouldAutoPlay=true
        return playerManager
    }()
    
    public lazy var controlView: ZFPlayerControlView = {
        let controlView=ZFPlayerControlView()
        controlView.fastViewAnimated=true
        controlView.autoHiddenTimeInterval = 5
        controlView.autoFadeTimeInterval = 0.5
        controlView.prepareShowLoading = true
        controlView.prepareShowControlView = false
        controlView.showTitle("iPhone X", coverURLString: "https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240", fullScreenMode: .automatic)
        controlView.backBtnClickCallback={[weak self] in
            self?.liveLive()
        }
        return controlView
    }()
    
    public var cutUrl = ""
    private var statusBarHiddenSelf:Bool=true

    ///修改状态栏
    public override var prefersStatusBarHidden: Bool {
        return statusBarHiddenSelf
    }
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(topView)
        config_data()
    }
    
    public override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        statusBarHiddenSelf=true
    }
    
    public override func viewWillDisappear(_ animated: Bool) {
        statusBarHiddenSelf=false
        self.setNeedsStatusBarAppearanceUpdate()
        super.viewWillDisappear(animated)
    }
    
    ///监听的回调 屏幕方向 发生变化的时候
    private func orientationChanged(_ notification:Notification){
        let deivce = UIDevice.current
        if deivce.orientation == .landscapeLeft || deivce.orientation == .landscapeRight {
            player.enterFullScreen(true, animated: false)
        }else{
            player.enterFullScreen(false, animated: false)
        }
    }
    
    public func config_data() {
        if let tempUrl = URL(string: cutUrl){
            player.assetURL=tempUrl
            ///视频准备完毕了 开始获取观看l进度
            player.playerLoadStateChanged={(media,state)->() in
                print("视频状态")
            }
        }
        player.orientationWillChange={(player,flag)->() in
            if flag{
                //                    weakSelf?.isStatusBarHidden=false
            }else{
                //                    weakSelf?.isStatusBarHidden=false
            }
        }
        playerManager.playerDidToEnd={[weak self](asset)->() in
            self?.player.stop()
            self?.liveLive()
        }
    }
    
    public override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation{
        return .none
    }
    
    public override var shouldAutorotate: Bool{
        return false
    }
    
    public override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        return .portrait
    }
    
       ///离开视频播放
    private func liveLive() {
//        playerManager.pause()
//        playerManager.stop()
//        navigationController?.popViewController(animated: true)
    }
}
